/*
 * Copyright (C) 2008-2013 TrinityCore <http://www.trinitycore.org/>
 * Copyright (C) 2010-2015 Rising Gods <http://www.rising-gods.de/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ScriptMgr.h"
#include "ScriptedCreature.h"
#include "azjol_nerub.h"

enum Spells
{
    SPELL_MIND_FLAY                               = 52586,
    H_SPELL_MIND_FLAY                             = 59367,
    SPELL_CURSE_OF_FATIGUE                        = 52592,
    H_SPELL_CURSE_OF_FATIGUE                      = 59368,
    SPELL_FRENZY                                  = 28747, //maybe 53361
    SPELL_SUMMON_SKITTERING_SWARMER               = 52438, //AOE Effect 140, maybe 52439
    SPELL_SUMMON_SKITTERING_SWARMER_1             = 52439, //Summon 3x 28735
    H_SPELL_ACID_SPLASH                           = 59363,
    SPELL_ACID_SPLASH                             = 52446,
    SPELL_CHARGE                                  = 16979, //maybe is another spell
    SPELL_BACKSTAB                                = 52540,
    SPELL_SHADOW_BOLT                             = 52534,
    H_SPELL_SHADOW_BOLT                           = 59357,
    SPELL_SHADOW_NOVA                             = 52535,
    H_SPELL_SHADOW_NOVA                           = 59358,
    SPELL_STRIKE                                  = 52532,
    SPELL_CLEAVE                                  = 49806,
    SPELL_ENRAGE                                  = 52470,
    SPELL_INFECTED_BITE                           = 52469,
    H_SPELL_INFECTED_BITE                         = 59364,
    SPELL_WEB_WRAP                                = 52086, 
    SPELL_BLINDING_WEBS                           = 52524,
    H_SPELL_BLINDING_WEBS                         = 59365,
    SPELL_POSION_SPRAY                            = 52493,
    H_SPELL_POSION_SPRAY                          = 59366
};

enum Creatures
{
    MOB_SKITTERING_SWARMER                        = 28735,
    MOB_SKITTERING_SWARMER_CONTROLLER             = 32593,
    MOB_SKITTERING_INFECTIOR                      = 28736
};

enum Yells
{
    SAY_AGGRO                                     = 0,
    SAY_SLAY                                      = 1,
    SAY_DEATH                                     = 2,
    SAY_SWARM                                     = 3,
    SAY_PREFIGHT                                  = 4,
    SAY_SEND_GROUP                                = 5
};

const Position SpawnPoint[8] =
{
    {566.164f, 682.087f, 769.079f, 2.21657f},
    {529.042f, 706.941f, 777.298f, 1.0821f},
    {489.975f, 671.239f, 772.131f, 0.261799f},
    {488.556f, 692.95f,  771.764f, 4.88692f},
    {553.34f,  640.387f, 777.419f, 1.20428f},
    {517.486f, 706.398f, 777.335f, 5.35816f},
    {504.01f,  637.693f, 777.479f, 0.506145f},
    {552.625f, 706.408f, 777.177f, 3.4383f}
};

enum Events
{
    EVENT_MIND_FLAY         = 1,
    EVENT_CURSE_FATIGUE     = 2,
    EVENT_SUMMON_ADDS       = 3
};

class boss_krik_thir : public CreatureScript
{
public:
    boss_krik_thir() : CreatureScript("boss_krik_thir") { }

    struct boss_krik_thirAI : public BossAI
    {
        boss_krik_thirAI(Creature* creature) : BossAI(creature, BOSS_KRIKTHIR_THE_GATEWATCHER) { }

        void EnterCombat(Unit* who) override
        {
            BossAI::EnterCombat(who);

            Talk(SAY_AGGRO);

            events.ScheduleEvent(EVENT_MIND_FLAY, 15*IN_MILLISECONDS);
            events.ScheduleEvent(EVENT_CURSE_FATIGUE, 12*IN_MILLISECONDS);
            events.ScheduleEvent(EVENT_SUMMON_ADDS, 15*IN_MILLISECONDS);

            me->CallForHelp(50.0f);

            if (instance)
            {
                if (ObjectGuid gashraGUID = instance->GetGuidData(DATA_WATCHER_GASHRA))
                    if (Creature* gashra = ObjectAccessor::GetCreature(*me, gashraGUID))
                        if (gashra->IsAlive())
                            gashra->AI()->AttackStart(who);

                if (ObjectGuid narjilGUID = instance->GetGuidData(DATA_WATCHER_NARJIL))
                    if (Creature* narjil = ObjectAccessor::GetCreature(*me, narjilGUID))
                        if (narjil->IsAlive())
                            narjil->AI()->AttackStart(who);

                if (ObjectGuid silthikGUID = instance->GetGuidData(DATA_WATCHER_SILTHIK))
                    if (Creature* silthik = ObjectAccessor::GetCreature(*me, silthikGUID))
                        if (silthik->IsAlive())
                            silthik->AI()->AttackStart(who);
            }
        }

        void Summon()
        {
            for (uint8 i=0; i<8; i++)
            {
                me->SummonCreature(RAND(MOB_SKITTERING_SWARMER,MOB_SKITTERING_INFECTIOR), SpawnPoint[i], TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 5*IN_MILLISECONDS);
                me->SummonCreature(MOB_SKITTERING_SWARMER, SpawnPoint[i], TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 5*IN_MILLISECONDS);
            }
        }

        void UpdateAI(uint32 diff) override
        {
            if (!UpdateVictim())
                return;

            if (me->HasUnitState(UNIT_STATE_CASTING))
                return;

            events.Update(diff);

            while (uint32 eventId = events.ExecuteEvent())
            {
                switch(eventId)
                {
                    case EVENT_SUMMON_ADDS:
                        Summon();
                        events.ScheduleEvent(EVENT_SUMMON_ADDS, 15*IN_MILLISECONDS);
                        break;
                    case EVENT_MIND_FLAY:
                        DoCastVictim(SPELL_MIND_FLAY);
                        events.ScheduleEvent(EVENT_MIND_FLAY, 15*IN_MILLISECONDS);
                        break;
                    case EVENT_CURSE_FATIGUE:
                        //WowWiki say "Curse of Fatigue-Kirk'thir will cast Curse of Fatigue on 2-3 targets periodically."
                        if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                            DoCast(target, SPELL_CURSE_OF_FATIGUE);
                        if (Unit* tankTarget = SelectTarget(SELECT_TARGET_RANDOM, 1, 100, true))
                            DoCast(tankTarget, SPELL_CURSE_OF_FATIGUE);
                        events.ScheduleEvent(EVENT_CURSE_FATIGUE, 10*IN_MILLISECONDS);
                        break;
                }
            }
            if (!me->HasAura(SPELL_FRENZY) && HealthBelowPct(10))
                DoCastSelf(SPELL_FRENZY, true);

            DoMeleeAttackIfReady();
        }

        void JustDied(Unit* killer) override
        {
            BossAI::JustDied(killer);
            Talk(SAY_DEATH);
        }

        void KilledUnit(Unit* who) override
        {
            if (who->GetTypeId() != TYPEID_PLAYER)
                return;

            Talk(SAY_SLAY);
        }

        void JustSummoned(Creature* summon) override
        {
            summon->GetMotionMaster()->MovePoint(0, me->GetPositionX(), me->GetPositionY(), me->GetPositionZ());
            summon->SetInCombatWithZone();
        }
    };

    CreatureAI* GetAI(Creature* creature) const override
    {
        return new boss_krik_thirAI(creature);
    }
};

class npc_skittering_infector : public CreatureScript
{
public:
    npc_skittering_infector() : CreatureScript("npc_skittering_infector") { }

    struct npc_skittering_infectorAI : public ScriptedAI
    {
        npc_skittering_infectorAI(Creature* creature) : ScriptedAI(creature) { }

        void JustDied(Unit* /*killer*/) override
        {
            DoCastAOE(DUNGEON_MODE(SPELL_ACID_SPLASH, H_SPELL_ACID_SPLASH), true);
        }
    };

    CreatureAI* GetAI(Creature* creature) const override
    {
        return new npc_skittering_infectorAI(creature);
    }
};

enum TrashEvents
{
    // Anubar Skrimisher
    EVENT_ANUBAR_CHARGE = 1,
    EVENT_BACKSTAB,

    // Anubar Shadowcaster
    EVENT_SHADOW_BOLT,
    EVENT_SHADOW_NOVA,

    // Anubar Warrior
    EVENT_STRIKE,
    EVENT_CLEAVE,

    // Watcher Gashra
    EVENT_WEB_WRAP_GASHRA,
    EVENT_INFECTED_BITE_GASHRA,

    // Watcher Narjil
    EVENT_WEB_WRAP_NARJIL,
    EVENT_INFECTED_BITE_NARJIL,
    EVENT_BINDING_WEBS,

    // Watcher Silthik
    EVENT_WEB_WRAP_SILTHIK,
    EVENT_INFECTED_BITE_SILTHIK,
    EVENT_POISON_SPRAY
};

class npc_anub_ar_skirmisher : public CreatureScript
{
    public:
        npc_anub_ar_skirmisher() : CreatureScript("npc_anub_ar_skirmisher") { }

        struct npc_anub_ar_skirmisherAI : public ScriptedAI
        {
            npc_anub_ar_skirmisherAI(Creature* creature) : ScriptedAI(creature) { }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                events.ScheduleEvent(EVENT_ANUBAR_CHARGE, 11000);
                events.ScheduleEvent(EVENT_BACKSTAB, 7000);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_ANUBAR_CHARGE:
                            if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                            {
                                ResetThreatList();
                                AddThreat(target, 1.0f);
                                DoCast(target, SPELL_CHARGE, true);
                            }
                            events.ScheduleEvent(EVENT_ANUBAR_CHARGE, 15000);
                            break;
                        case EVENT_BACKSTAB:
                            DoCastVictim(SPELL_BACKSTAB);
                            events.ScheduleEvent(EVENT_BACKSTAB, 12000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }
        private:
            EventMap events;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_anub_ar_skirmisherAI(creature);
        }
};

class npc_anub_ar_shadowcaster : public CreatureScript
{
    public:
        npc_anub_ar_shadowcaster() : CreatureScript("npc_anub_ar_shadowcaster") { }

        struct npc_anub_ar_shadowcasterAI : public ScriptedAI
        {
            npc_anub_ar_shadowcasterAI(Creature* creature) : ScriptedAI(creature) { }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                events.ScheduleEvent(EVENT_SHADOW_BOLT, 6000);
                events.ScheduleEvent(EVENT_SHADOW_NOVA, 15000);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_SHADOW_BOLT:
                            if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                                DoCast(target, SPELL_SHADOW_BOLT, true);
                            events.ScheduleEvent(EVENT_SHADOW_BOLT, 15000);
                            break;
                        case EVENT_SHADOW_NOVA:
                            DoCastVictim(SPELL_SHADOW_NOVA, true);
                            events.ScheduleEvent(EVENT_SHADOW_NOVA, 17000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }

        private:
            EventMap events;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_anub_ar_shadowcasterAI(creature);
        }
};

class npc_anub_ar_warrior : public CreatureScript
{
    public:
        npc_anub_ar_warrior() : CreatureScript("npc_anub_ar_warrior") { }

        struct npc_anub_ar_warriorAI : public ScriptedAI
        {
            npc_anub_ar_warriorAI(Creature* creature) : ScriptedAI(creature) { }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                events.ScheduleEvent(EVENT_CLEAVE, 11000);
               events.ScheduleEvent(EVENT_STRIKE, 6000);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_CLEAVE:
                            DoCastVictim(SPELL_STRIKE, true);
                            events.ScheduleEvent(EVENT_CLEAVE, 15000);
                            break;
                        case EVENT_STRIKE:
                            DoCastVictim(SPELL_CLEAVE, true);
                            events.ScheduleEvent(EVENT_STRIKE, 17000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }

        private:
            EventMap events;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_anub_ar_warriorAI(creature);
        }
};

class npc_watcher_gashra : public CreatureScript
{
    public:
        npc_watcher_gashra() : CreatureScript("npc_watcher_gashra") { }

        struct npc_watcher_gashraAI : public ScriptedAI
        {
            npc_watcher_gashraAI(Creature* creature) : ScriptedAI(creature) 
            {
                _instance = creature->GetInstanceScript();
            }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                DoCastSelf(SPELL_ENRAGE, true);
                events.ScheduleEvent(EVENT_WEB_WRAP_GASHRA, 11000);
                events.ScheduleEvent(EVENT_INFECTED_BITE_GASHRA, 4000);
            }

            void JustDied(Unit* /*killer*/) override
            {
                Creature* krikthir = ObjectAccessor::GetCreature(*me, _instance->GetGuidData(DATA_KRIKTHIR_THE_GATEWATCHER));
                if (krikthir && krikthir->IsAlive())
                    krikthir->AI()->Talk(SAY_PREFIGHT);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_WEB_WRAP_GASHRA:
                            // We will teleport the NPC to his position, so he will stop moving - Dont doing this will cause instant casts of WebWrap
                            me->NearTeleportTo(me->GetPositionX(), me->GetPositionY(), me->GetPositionZ(), me->GetOrientation());
                            if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                                DoCast(target, SPELL_WEB_WRAP, true);
                            events.ScheduleEvent(EVENT_WEB_WRAP_GASHRA, 17000);
                            break;
                        case EVENT_INFECTED_BITE_GASHRA:
                            DoCastVictim(SPELL_INFECTED_BITE, true);
                            events.ScheduleEvent(EVENT_INFECTED_BITE_GASHRA, 15000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }

        private:
            EventMap events;
            InstanceScript* _instance;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_watcher_gashraAI(creature);
        }
};

class npc_watcher_narjil : public CreatureScript
{
    public:
        npc_watcher_narjil() : CreatureScript("npc_watcher_narjil") { }

        struct npc_watcher_narjilAI : public ScriptedAI
        {
            npc_watcher_narjilAI(Creature* creature) : ScriptedAI(creature) 
            {
                _instance = creature->GetInstanceScript();
            }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                events.ScheduleEvent(EVENT_WEB_WRAP_NARJIL, 11000);
                events.ScheduleEvent(EVENT_INFECTED_BITE_NARJIL, 4000);
                events.ScheduleEvent(EVENT_BINDING_WEBS, 17000);
            }

            void JustDied(Unit* /*killer*/) override
            {
                Creature* krikthir = ObjectAccessor::GetCreature(*me, _instance->GetGuidData(DATA_KRIKTHIR_THE_GATEWATCHER));
                if (krikthir && krikthir->IsAlive())
                    krikthir->AI()->Talk(SAY_PREFIGHT);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_WEB_WRAP_NARJIL:
                            // We will teleport the NPC to his position, so he will stop moving - Dont doing this will cause instant casts of WebWrap
                            me->NearTeleportTo(me->GetPositionX(), me->GetPositionY(), me->GetPositionZ(), me->GetOrientation());
                            if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                                DoCast(target, SPELL_WEB_WRAP, true);
                            events.ScheduleEvent(EVENT_WEB_WRAP_NARJIL, 15000);
                            break;
                        case EVENT_INFECTED_BITE_NARJIL:
                            DoCastVictim(SPELL_INFECTED_BITE, true);
                            events.ScheduleEvent(EVENT_INFECTED_BITE_NARJIL, 11000);
                            break;
                        case EVENT_BINDING_WEBS:
                            DoCastVictim(SPELL_BLINDING_WEBS, true);
                            events.ScheduleEvent(EVENT_BINDING_WEBS, 17000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }

        private:
            EventMap events;
            InstanceScript* _instance;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_watcher_narjilAI(creature);
        }
};

class npc_watcher_silthik : public CreatureScript
{
    public:
        npc_watcher_silthik() : CreatureScript("npc_watcher_silthik") { }

        struct npc_watcher_silthikAI : public ScriptedAI
        {
            npc_watcher_silthikAI(Creature* creature) : ScriptedAI(creature) 
            {
                _instance = creature->GetInstanceScript();
            }

            void Reset() override
            {
                events.Reset();
            }

            void EnterCombat(Unit* /*who*/) override
            {
                events.ScheduleEvent(EVENT_WEB_WRAP_SILTHIK, 11000);
                events.ScheduleEvent(EVENT_INFECTED_BITE_SILTHIK, 4000);
                events.ScheduleEvent(EVENT_POISON_SPRAY, 15000);
            }

            void JustDied(Unit* /*killer*/) override
            {
                Creature* krikthir = ObjectAccessor::GetCreature(*me, _instance->GetGuidData(DATA_KRIKTHIR_THE_GATEWATCHER));
                if (krikthir && krikthir->IsAlive())
                    krikthir->AI()->Talk(SAY_PREFIGHT);
            }

            void UpdateAI(uint32 diff) override
            {
                if (!UpdateVictim())
                    return;

                events.Update(diff);

                if (me->HasUnitState(UNIT_STATE_CASTING))
                    return;

                while (uint32 eventId = events.ExecuteEvent())
                {
                    switch (eventId)
                    {
                        case EVENT_WEB_WRAP_SILTHIK:
                            // We will teleport the NPC to his position, so he will stop moving - Dont doing this will cause instant casts of WebWrap
                            me->NearTeleportTo(me->GetPositionX(), me->GetPositionY(), me->GetPositionZ(), me->GetOrientation());
                            if (Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 100, true))
                                DoCast(target, SPELL_WEB_WRAP, true);
                            events.ScheduleEvent(EVENT_WEB_WRAP_SILTHIK, 15000);
                            break;
                        case EVENT_INFECTED_BITE_SILTHIK:
                            DoCastVictim(SPELL_INFECTED_BITE, true);
                            events.ScheduleEvent(EVENT_INFECTED_BITE_SILTHIK, 11000);
                            break;
                        case EVENT_POISON_SPRAY:
                            DoCastVictim(SPELL_POSION_SPRAY, true);
                            events.ScheduleEvent(EVENT_POISON_SPRAY, 17000);
                            break;
                        default:
                            break;
                    }
                }

                DoMeleeAttackIfReady();
            }

        private:
            EventMap events;
            InstanceScript* _instance;
        };

        CreatureAI* GetAI(Creature* creature) const override
        {
            return new npc_watcher_silthikAI(creature);
        }
};

class achievement_watch_him_die : public AchievementCriteriaScript
{
public:
    achievement_watch_him_die() : AchievementCriteriaScript("achievement_watch_him_die") { }

    bool OnCheck(Player* /*player*/, Unit* target) override
    {
        if (!target)
            return false;

        InstanceScript* instance = target->GetInstanceScript();
        Creature* Watcher[3];
        if (!instance)
            return false;

        for (uint8 n = 0; n < 3; ++n)
        {
            Watcher[n] = ObjectAccessor::GetCreature(*target, instance->GetGuidData(DATA_WATCHER_GASHRA + n));
            if (Watcher[n] && !Watcher[n]->IsAlive())
                return false;
        }
        return true;
    }
};

void AddSC_boss_krik_thir()
{
    new boss_krik_thir();
    new npc_skittering_infector();
    new npc_anub_ar_skirmisher();
    new npc_anub_ar_shadowcaster();
    new npc_watcher_gashra();
    new npc_anub_ar_warrior();
    new npc_watcher_silthik();
    new npc_watcher_narjil();
    new achievement_watch_him_die();
}
